package com.wf.course.service.impl;


import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.utils.dateUtil.DateUtil;
import com.utils.page.PageBean;
import com.utils.pay.BillUtil;
import com.wf.course.dao.IBillMapper;
import com.wf.course.dao.IGoodsOrderMapper;
import com.wf.course.model.bill.Bill;
import com.wf.course.model.goods.GoodsOrder;
import com.wf.course.service.IBillService;

@Service("billServiceImpl")
@Transactional
public class IBillServiceImpl implements IBillService {
	
	@Autowired
	private IBillMapper mapper;
	@Autowired
	private IGoodsOrderMapper gorderMapper;
	
	private Logger log = Logger.getLogger(IBillServiceImpl.class);

	@Override
	public void saveBill() {
		// TODO Auto-generated method stub
		 //查询昨天的对账单，并返回
		 String yesterday  = DateUtil.getYesterday("yyyyMMdd");
		 String result = BillUtil.getBills(yesterday);
		 if(result.startsWith("<xml>")){//查询日期为当天时，错误信息提示日期无效
	            System.out.println(result);
	            System.out.println("无订单");
	      }else {  
	           String tradeMsg =  result.substring(result.indexOf("`"));
	           String tradeInfo =  tradeMsg.substring(0,tradeMsg.indexOf("总"));
	           String tradeTotalMsg =tradeMsg.substring(tradeMsg.indexOf("总"));
	           String tradeTotalInfo =tradeTotalMsg.substring(tradeTotalMsg.indexOf("`"));
	         
	           System.out.println(tradeInfo);
	           
	          List<Bill> billList =  BillUtil.fmtString(tradeInfo);
	          if(billList.size()>0){
	        	  //保存账单表
	        	  int saveFlag = mapper.saveBill(billList);
	        	  log.info("保存账单数量:"+billList.size());
	        	  //账单保存成功后，根据订单号，更新订单时间，交易单号
	        	  if(saveFlag>0){
	        		  
	        		  for(Bill bl:billList){
	        			
	        			  if(bl!=null && bl.getTradestatus().equals("SUCCESS")){
	        				 //根据订单号，查询订单； 更新订单支付时间和交易单号
	        				 GoodsOrder gorder = gorderMapper.queryGoodsOrderByOrderCode(bl.getBzorder());
	        				 if(gorder!=null ){
	        					 gorder.setOrderPayTime(bl.getTradetime());
	        					 gorder.setTransaction_id(bl.getWxorder());
	        					//更新支付时间
	        					 gorderMapper.updatePayTime(gorder);
	        				 }
	        				  
	        			 }
	        		  }
	        	  }
	        	  
	          }
	        
	      }
	}

	@Override
	public void indexInfo(PageBean<Bill> pageBean) {
		// TODO Auto-generated method stub
		pageBean.setResults(this.mapper.queryList(pageBean));
		pageBean.setTotalCount(this.mapper.getTotalCount(pageBean));
		
	}

	@Override
	public List<Bill> getBills(PageBean<Bill> pageBean) {
		// TODO Auto-generated method stub
		return mapper.getBills(pageBean);
	}

	@Override
	public void updateOrderPayTime() {
		// TODO Auto-generated method stub
		//查询微信账单
		List<Bill> billList= mapper.getBillList();
		
		  for(Bill bl:billList){
  			
 			 if(bl!=null && bl.getTradestatus().equals("SUCCESS")){
 				  
 				 //根据订单号，查询订单； 更新订单支付时间和交易单号
				 GoodsOrder gorder = gorderMapper.queryGoodsOrderByOrderCode(bl.getBzorder());
				 
				 if(gorder!=null ){
					 gorder.setOrderPayTime(bl.getTradetime());
					 gorder.setTransaction_id(bl.getWxorder());
					//更新支付时间
					 gorderMapper.updatePayTime(gorder);
				 }
 				  
 			 }
 		  }
	}
 
}
